package de.wepner.texteditor.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;


/**
 * <p>Contains several functions for accessing the file system and files.</p>
 * <p>No function does any user requests, e. g. ask if a file should be
 * overwritten, so the caller should only call this functions if the
 * action relly should be performed. These functions should only be used
 * by the commands.</p>
 * 
 * @author Richard Wepner
 */
public class IOUtils {
    
    
    public static String loadFile(File file) {
        
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        if (file.exists() && file.isFile() && file.canRead()) {
            
            try (FileInputStream stream = new FileInputStream(file)) {
                byte[] data = new byte[stream.available()];
                stream.read(data);
                
                try {
                    stream.close();
                } catch (IOException closeException) {
                    
                }
                return new String(data, Charset.forName("UTF-8"));
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }
    
    public static boolean saveFile(String content, File file) {
        
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (file.exists() && file.isFile() && file.canWrite()) {
            try (FileWriter writer = new FileWriter(file)) {
                writer.write(content);
                writer.close();
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
                return true;
            } catch (IOException exc) {
                return false;
            }
        } else {
            return false;
        }
    }
    
    
    private static File getOrCreateAppDataDirectory() {
        
        final File directory = IOUtils.getAppDataDirectory();
        if (directory.exists()) {
            if (directory.isDirectory()) {
                return directory;
            } else {
                if (directory.delete() && directory.mkdir()) {
                    return directory;
                } else {
                    return null;
                }
            }
        } else {
            if (directory.mkdirs()) {
                return directory;
            } else {
                return null;
            }
        }
    }
    
    private static File getAppDataDirectory() {
        
        return new File(System.getenv("APPDATA") + File.separator + "Texteditor");
    }
}
